﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using System.Web.Mvc;

namespace SalesSys.WebCore
{
    public static class EnumExt
    {
        //ICache<Type, List<SelectListItem>> cache =new  WebCache<Type, List<SelectListItem>>();

        /// <summary>
        /// 将枚举描述特性，转化为SelectListItem,并将传入的枚举值对应的项设为选中 selectListItem.Selected=true
        /// </summary>
        /// <param name="valueEnum"></param>
        /// <returns></returns>
        public static List<SelectListItem> ToSelectListItems(this Enum valueEnum)
        {
            Type enumType = valueEnum.GetType();
            var values = enumType.GetEnumNames();
            var list = new List<SelectListItem>();
            foreach (var value in values)
            {
                var selectListItem = new SelectListItem();
                var attr =
                    Attribute.GetCustomAttribute(enumType.GetField(value), typeof(DescriptionAttribute)) as
                        DescriptionAttribute;
                selectListItem.Text = attr != null ? attr.Description : value;
                selectListItem.Value = value;//((int)Enum.Parse(enumType,value)).ToString();
                selectListItem.Selected = selectListItem.Value == valueEnum.ToString();//Convert.ToInt32( valueEnum).ToString();
                list.Add(selectListItem);
            }
            return list;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="valueEnum"></param>
        /// <returns></returns>
        public static List<SelectListItem> ToIntSelectListItem(this Enum valueEnum)
        {
            Type enumType = valueEnum.GetType();
            var values = enumType.GetEnumNames();
            var list = new List<SelectListItem>();
            foreach (var value in values)
            {
                var selectListItem = new SelectListItem();
                var attr =
                    Attribute.GetCustomAttribute(enumType.GetField(value), typeof(DescriptionAttribute)) as
                        DescriptionAttribute;
                selectListItem.Text = attr != null ? attr.Description : value;
                selectListItem.Value = ((int)Enum.Parse(enumType, value)).ToString();
                selectListItem.Selected = selectListItem.Value == Convert.ToInt32(valueEnum).ToString();
                list.Add(selectListItem);
            }
            return list;
        }

        /// <summary>
        /// 返回枚举列表
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public static List<SelectListItem> ToSelectListItems(Type enumType, string intSelected = null)
        {
            var values = enumType.GetEnumNames();
            var list = new List<SelectListItem>();
            foreach (var value in values)
            {
                var selectListItem = new SelectListItem();
                var attr = GetDescInfo(enumType.GetField(value));
                selectListItem.Text = attr != null ? attr : value;
                int itemInt = (int)Enum.Parse(enumType, value);
                selectListItem.Value = itemInt.ToString();
                selectListItem.Selected =selectListItem.Value == intSelected;
                list.Add(selectListItem);
            }
            return list;
        }
        /// <summary>
        /// 获取描述信息
        /// </summary>
        /// <param name="valueEnum"></param>
        /// <returns></returns>
        public static string GetDescription(this Enum valueEnum)
        {
            Type enumType = valueEnum.GetType();
            var name = Enum.GetName(enumType, valueEnum);
            var info = enumType.GetField(name);
            return GetDescInfo(info);
        }

        private static string GetDescInfo(FieldInfo info)
        {
            var attr1 = Attribute.GetCustomAttribute(info, typeof(DisplayAttribute)) as DisplayAttribute;
            if (attr1 != null)
            {
                return attr1.Name;
            }
            var attr = Attribute.GetCustomAttribute(info, typeof(DescriptionAttribute)) as DescriptionAttribute;

            return attr == null ? null : attr.Description;
        }

    }
}
